<!-- 会员管理界面 -->
<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<script type="text/javascript" charset="utf-8">
	$(function() {
		$.extend($.fn.validatebox.defaults.rules, {
		     phoneOrMobile: {//验证手机或电话
				    	  validator: function (value) {
				    	     return /^(13|15|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
				    	 },
				    	    message: '请填入手机或电话号码,如13688888888或020-8888888'
				    },
			 chinese : {// 验证中文
	                     validator : function(value) {
				    	     return /^[\u0391-\uFFE5]+$/i.test(value);
				    	  },
				    	    message : '请输入中文'
				    },
			 jretailUpperLimit:{ //验证是否为{0}到{1}之间的最多为两位小数的数字
		                validator:function(value,param){  
		                    if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){  
		                        return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);  
		                    }else{  
		                        return false;  
		                    }  
		                },  
		                message:'请输入{0}到{1}之间的最多俩位小数的数字'  
		            },  
	          floatOrInt: {// 验证是否为小数或整数
	        			validator: function (value) {
	        			      return /^(\d{1,20}(,\d\d\d)*(\.\d{1,20}(,\d\d\d)*)?|\d+(\.\d+))?$/i.test(value);
	        			   },
	        			message: '请输入数字，并保证格式正确'
	        		 }

		});
		editRow=undefined;
		$('#admin_vipgl_datagrid').datagrid({
			url : ' ${pageContext.request.contextPath}/vipAction!datagrid.action ',
			title : '',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			fit : true,
			fitColumns : true,
			nowarp : false,
			border : false,
			idField : 'id',
			checkOnSelect:false,
			selectOnCheck:false,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 100,
				checkbox:true
			}, {
				field : 'cardid',
				title : '卡号',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填'
						}
			           }
			}, {
				field : 'name',
				title : '姓名',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType:'chinese'
						}
			           }
			}, {
				field : 'telephone',
				title : '联系方式',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType:'phoneOrMobile'
						}
			           }
			}, {
				field : 'gender',
				title : '性别',
				width : 100,
				editor:{
					type:'combobox',
					options:{
						panelHeight:'auto',
						editable:false,
						valueField: 'label',
						textField: 'value',
						data: [{
							label: '男',
							value: '男'
						},{
							label: '女',
							value: '女'
						}],
						required:true,
						missingMessage:'该项必填'
						}
			           }
			}, {
				field : 'discount',
				title : '折扣',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType:'jretailUpperLimit[0,1]'
						}
			           }
			}, {
				field : 'integration',
				title : '会员积分',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType:'floatOrInt'
						}
			           }
			}, {
				field : 'createdatetime',
				title : '创建时间',
				width : 100,
				sortable:true,
				editor:{
					type:'datebox',
					options:{
						editable:false
						}
			           }
			}, {
				field : 'updatedatetime',
				title : '最后修改时间',
				width : 100,
				sortable:true,
				editor:{
					type:'datebox',
					options:{
						//required:true,
						editable:false
						}
			           }
			} ] ],
			toolbar:'#toolbar',
			onAfterEdit:function(rowIndex,rowData,changes){
				var inserted=$('#admin_vipgl_datagrid').datagrid('getChanges','inserted');
				var updated=$('#admin_vipgl_datagrid').datagrid('getChanges','updated');
				if (inserted.length < 1 && updated.length < 1) {
					editRow = undefined;
					$('#admin_vipgl_datagrid').datagrid('unselectAll');
					return;
				}
				var url=' ';
				if(inserted.length>0){
					url='vipAction!add.action';
				}
				if(updated.length>0){
					url='vipAction!editor.action';
				}
				$.ajax({
					url:url,
					data:rowData,
					dataType:'json',
					success:function(r){
						if(r&&r.success){
							$('#admin_vipgl_datagrid').datagrid('acceptChanges');
							$('#admin_vipgl_datagrid').datagrid('load');
						    $.messager.show({
						    	title:'成功',
						    	msg:r.msg
						    	});
						}else{
							$('#admin_vipgl_datagrid').datagrid('rejectChanges');
						    $.messager.alert('错误',r.msg,'error');
						}
						editRow=undefined;
						$('#admin_vipgl_datagrid').datagrid('unselectAll');
					}
				});
			},
			onDblClickRow:function(rowIndex,rowData){
				changeEditorEditRow();
				if(editRow!=undefined){
					$('#admin_vipgl_datagrid').datagrid('endEdit',editRow);	
				}
				if(editRow==undefined){
				    $('#admin_vipgl_datagrid').datagrid('beginEdit',rowIndex);
				    editRow=rowIndex;
				    $('#admin_vipgl_datagrid').datagrid('unselectAll');
				}
			} 
		});
		addFun=function(){ //添加
			  if(editRow!=undefined){
					$('#admin_vipgl_datagrid').datagrid('endEdit',editRow);
				}  
				if(editRow==undefined){
					$('#admin_vipgl_datagrid').datagrid('unselectAll');	
				    changeEditorAddRow();
				    $('#admin_vipgl_datagrid').datagrid('insertRow',{
					       index:0,
					       row:{}
					 });
				$('#admin_vipgl_datagrid').datagrid('beginEdit',0);
				$('#admin_vipgl_datagrid').datagrid('unselectAll');
				editRow=0;
			    }
		};
		editFun=function(){ //编辑
			var rows=$('#admin_vipgl_datagrid').datagrid('getChecked');
			if(rows.length==1){
				changeEditorEditRow();
				if(editRow!=undefined){
					$('#admin_vipgl_datagrid').datagrid('endEdit',editRow);	
				}
				if(editRow==undefined){
					var index= $('#admin_vipgl_datagrid').datagrid('getRowIndex',rows[0]);
				    $('#admin_vipgl_datagrid').datagrid('beginEdit',index);
				    editRow=index;
				    $('#admin_vipgl_datagrid').datagrid('unselectAll');
				}
			}
			if(rows.length>1){
				$.messager.alert('警告','同一时间只能修改一条记录!','warning');
			}if(rows.length<1){
				$.messager.alert('警告','请选择要修改的记录!','warning');
			}
		};
		delFun=function(){ //删除
			var rows=$('#admin_vipgl_datagrid').datagrid('getChecked');
			if(rows.length>0){
				$.messager.confirm("请确认","您确定要删除当前所选择的记录吗?",function(b){
					if(b){
						var ids=[];
						for(var i=0;i<rows.length;i++){
							ids.push(rows[i].id);
						}
						//console.info(ids.join(','));
						$.ajax({
							url:'vipAction!del.action',
							data:{
								ids:ids.join(',')
								},
							dataType:'json',
							success:function(r){
								if(r&&r.success){
									$('#admin_vipgl_datagrid').datagrid('load');
									$('#admin_vipgl_datagrid').datagrid('unselectAll');
								    $.messager.show({
								    	title:'提示',
								    	msg:r.msg
								    	});
								}else{
									$('#admin_vipgl_datagrid').datagrid('rejectChanges');
									$('#admin_vipgl_datagrid').datagrid('unselectAll');
								    $.messager.alert('错误',r.msg,'error');
								}
							}
						});
					}
				});
			}else{
				  $.messager.alert('警告','未选中任何记录,请选择要删除的记录!','warning');			
			} 
		};
		saveFun=function(){ //保存
			$('#admin_vipgl_datagrid').datagrid('endEdit',editRow);
		};
		undoEdit=function(){ //取消编辑
			editRow=undefined;
			$('#admin_vipgl_datagrid').datagrid('rejectChanges');
			$('#admin_vipgl_datagrid').datagrid('unselectAll');
		};
		unSelect=function(){ //取消选中
			$('#admin_vipgl_datagrid').datagrid('unselectAll');
		};
		refresh=function(){ //刷新
			 $('#admin_vipgl_datagrid').datagrid('reload');
		};
		$('#admin_vipgl_searchbox').searchbox({ //搜索框
		    searcher:function(value,name){
		    	searchFun();
		    },
		    menu:'#mm',
		    prompt:'可进行模糊查询'
		});
		/* $('#admin_yhgl_datagrid').datagrid('enableFilter'); */
		changeEditorAddRow=function(){
			$('#admin_vipgl_datagrid').datagrid('removeEditor',['createdatetime','updatedatetime']);
		};
		changeEditorEditRow=function(){
			$('#admin_vipgl_datagrid').datagrid('addEditor',[{
				field:'createdatetime',
				editor:{
					type:'datebox',
					options:{
						editable:false
						}
			         }
			}]);
		};
	});
	
	function searchFun() {
		//console.info(serializeObject($('#admin_yhgl_searchForm')));
		$('#admin_vipgl_datagrid').datagrid('load',serializeObject($('#admin_vipgl_searchForm')));
	}
	function clearFun() {
		$('#admin_vipgl_datagrid').datagrid('load', {});
	}
</script>
<div id="admin_vipgl_layout" class="easyui-layout" data-options="border:false,fit:true">
<div region="north" data-options="border:false,title:'过滤信息'" style="height:75px;overflow:hidden;">
<form id="admin_vipgl_searchForm">
<div style="float:right;margin:5px;"><input id="admin_vipgl_searchbox" style="width:300px;"></input></div><br/>
<div style="float:left;margin:5px;"><label>创建时间&nbsp;:&nbsp;从&nbsp;</label><input name="createdatetimeStart"  class="easyui-datebox" /><label>&nbsp;至&nbsp;</label><input name="createdatetimeEnd" class="easyui-datebox" />
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();" >查询</a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="clearFun();">回显所有</a></div>
</form>
<div id="mm" style="width:120px">
    <div data-options="name:'cardid',iconCls:'icon-ok'">卡号</div>
    <div data-options="name:'name',iconCls:'icon-ok'">姓名</div>
    <div data-options="name:'telephone',iconCls:'icon-ok'">联系方式</div>
</div>
</div>
<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<shiro:hasPermission name="sys:vipgl:add">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addFun();">添加</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:vipgl:del">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="delFun();">删除</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:vipgl:edit">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editFun();">修改</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:vipgl:edit"><td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveFun();">保存</a></td></shiro:hasPermission>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="undoEdit();">取消编辑</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="unSelect();">取消选中</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="refresh();">刷新</a></td>
			</tr>
		</table>
	</div>
<div region="center" data-options="border:false">
<table id="admin_vipgl_datagrid"></table>
</div>
</div>


